﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CoreShop.Model;
using CoreShop.Services;
using Sunny.UI;
using CoreShop.Library;

namespace CoreShop.App
{
    public partial class frmDeskOpen : BaseForm
    {
        long deskId = 0;
        public long orderId = 0;
        private List<corecmsfoodtag> tagList = null;

        public frmDeskOpen(long _deskId)
        {
            deskId = _deskId;
            InitializeComponent();
            txtPersons.TextBox.GotFocus += txtPersons_GotFocus;
            txtMember.TextBox.GotFocus += txtMember_GotFocus;
        }

        private void txtPersons_GotFocus(object sender, System.EventArgs e)
        {
            numPad.SetTextBox((UIEdit)sender, UITextBox.UIEditType.Integer, 0);
        }

        private void txtMember_GotFocus(object sender, System.EventArgs e)
        {
            numPad.SetTextBox((UIEdit)sender, UITextBox.UIEditType.String, 0);
        }

        private void frmTableOpen_Load(object sender, EventArgs e)
        {
            var deskInfo = new corecmsfooddeskServices().GetById(deskId);
            if (deskInfo == null)
            {
                UIMessageBox.Show("桌台信息不存在。", "系统出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                this.Close();
            }
            pnlDeskName.Text = deskInfo.name;
            var orders = new corecmsfoodorderingmasterServices().QueryListByDeskId(deskId);
            if ((orders!= null) && (orders.Count > 0))
            {
                string numbwrStr = CommFunc.GetDeskNumber(orders);
                this.Text = "拼台开台";
                pnlDeskName.Text = deskInfo.name + "（拼台：" + deskInfo.name + "-" + numbwrStr + ")";
            }
            //tagList = new corecmsfoodtagServices().QueryAsync((int)GlobalEnumVars.FoodTag.OpenTableTag);
            //mbtGetRemark.Clear();
            //mbtGetRemark.MaxSelected = 10;
            //mbtGetRemark.AutoWidth = true;
            //foreach (var item in tagList)
            //{
            //    mbtGetRemark.Add(item.content, item.id);
            //}
        }

        private void txtKey_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                double fInput;
                if (!double.TryParse(txtPersons.Text, out fInput))
                {
                    UIMessageBox.Show("请输入有效的数字。", "系统出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                    return;
                }
                DoOK();
            }
        }

        private void txtSendKey_Validating(object sender, CancelEventArgs e)
        {
            double fInput;
            if (!double.TryParse(txtPersons.Text, out fInput))
            {
                MessageBox.Show("请输入有效的数字！");
                e.Cancel = true;
            }
        }

        private void frmInputTxt_Shown(object sender, EventArgs e)
        {
            txtPersons.Focus();
        }        

        private void mbtGetRemark_SelectedChanged(object sender, EventArgs e)
        {
            UIMultiButtons button = (UIMultiButtons)sender;
            UIButton btn = button.SelectedBtn;

            if (btn != null)
            {
                string btnTxt = btn.Text;
                bool btnSelect = btn.Selected;
                if (btnSelect)
                {
                    if (!string.IsNullOrEmpty(txtRemark.Text)) txtRemark.Text = txtRemark.Text + ",";
                    txtRemark.Text = txtRemark.Text + btnTxt;
                }
                else {
                    txtRemark.Text =  txtRemark.Text.ToString().Replace(btnTxt, "").Replace(",,",",").TrimStart(',').TrimEnd(',');
                }
            }
        }

        private void numPad_OKButtonClick(object sender, string text, int intvalue, double doublevalue)
        {
            int person;
            string sInput = txtPersons.Text;
            if (string.IsNullOrEmpty(sInput)) sInput = "0";
            if (!int.TryParse(sInput, out person))
            {
                UIMessageBox.Show("请输入合法的人数。", "出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }
            if (person < 0)
            {
                UIMessageBox.Show("输入的数不能为负数。", "出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }

            var orders = new corecmsfoodorderingmasterServices().QueryListByDeskId(deskId);
            string numbwrStr = CommFunc.GetDeskNumber(orders);
            string orderNo = ProcessUtil.GetOrderNo((int)GlobalEnumVars.SerialNumberType.订单编号);
            var orderingMaster = new corecmsfoodorderingmasterServices().GreateOrderingMaster(AppConstVars.sysUser, deskId, AppConstVars.sysUser.id, person, numbwrStr, AppConstVars.classId, orderNo);
            if (orderingMaster == null)
            {
                UIMessageBox.Show("开台失败。", "出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }
            orderId = orderingMaster.orderId;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
